/*
 * Map-Data-Layout: .... hard to describe :D ...
 *
 *    [map]
 *      |
 *      |
 *      |------[mapLayers]
 *      |           |------------[layer_a_id]
 *      |           |                |----------[tile_a_id]
 *      |           |                |             |------------[imageSources_id, posX, posY]
 *      |           |                |
 *      |           |                |----------[tile_b_id]
 *      |           |                              |------------[imageSources_id, posX, posY]
 *      |           |
 *      |           |------------[layer_b_id]
 *      |                            |----------[tile_a_id]
 *      |                            |             |------------[imageSources_id, posX, posY]
 *      |                            |
 *      |                            |----------[tile_b_id]
 *      |                                          |------------[imageSources_id, posX, posY]
 *      |
 *      |
 *      |------[imageSources]
 *      |           |------------[imageSources_id_a]
 *      |           |                   |----------------[pathtoImage]
 *      |           |------------[imageSources_id_b]
 *      |                               |----------------[pathtoImage]
 *      |
 *      |
 *      |------[offsetFactor]
 *                  |------------[layer_a_id]
 *                  |                 |----------[offsetFactor value]
 *                  |------------[layer_b_id]
 *                                    |----------[offsetFactor value]
 */


function TileMap() {}
TileMap.prototype = new Entity;

TileMap.prototype.map = new Array();
TileMap.prototype.map.imageSources = new Array();
TileMap.prototype.map.mapLayers = new Array();
TileMap.prototype.map.offsetFactor = new Array();






TileMap.prototype.init = function()
{

  this.map.mapLayers[0] = new Array();
  this.map.mapLayers[1] = new Array();
  this.map.mapLayers[2] = new Array();
  this.map.mapLayers[3] = new Array();
  
  this.map.mapLayers[0][0] = new Array(0, 0, 0);
  this.map.mapLayers[1][0] = new Array(1, 0, 0);
  this.map.mapLayers[2][0] = new Array(2, 0, 0);

  for (var i = 0; i < 20; i++) {
      this.map.mapLayers[3][i] = new Array(3, i * 128, 448);
  }

  this.map.imageSources[0] = "images/tiles/sky/bglayer01_1024x512.jpg";
  this.map.imageSources[1] = "images/tiles/sky/bglayer02_1024x512.png";
  this.map.imageSources[2] = "images/tiles/sky/bglayer03_1024x512.png";
  this.map.imageSources[3] = "images/tiles/sky/rock01_128x128.png";

  this.map.offsetFactor[0] = 0.001;
  this.map.offsetFactor[1] = 0.01;
  this.map.offsetFactor[2] = 0.1;
  this.map.offsetFactor[3] = 1;

}


TileMap.prototype.loadMap = function()
{
  // this.map = map;
  for (var i=0; i <= this.map.imageSources.length; i++) {
    this.addEntity("Tile");
    this.entities["Tile"][i].context = RTE.RenderManager.Viewport.context;
    this.entities["Tile"][i].loadImageSource(this.map.imageSources[i]);
  }
}

TileMap.prototype.update = function()
{
}


TileMap.prototype.render = function()
{
  for (var i = 0; i < this.map.mapLayers.length; i++)
  {
    for (var k = 0; k < this.map.mapLayers[i].length; k++)
    {
      
      // calulate the new offset before render process instead of creating a new entity for each tile
      var currentPosX = this.map.mapLayers[i][k][1] + (RTE.GameManager.PlayerCamera.offsetX * this.map.offsetFactor[i]);
      var currentPosY = this.map.mapLayers[i][k][2] + (RTE.GameManager.PlayerCamera.offsetY * this.map.offsetFactor[i]);

      // render the tile
      this.entities["Tile"][this.map.mapLayers[i][k][0]].render(currentPosX, currentPosY);
//      this["Tile" + this.map.imageSources[0]][this.mapLayers[i][this.k][0]].draw(Viewport.offsetX * this.offsetFactor[i] + this.mapLayers[i][this.k][1], Viewport.offsetY * this.offsetFactor[this.i] + this.mapLayers[this.i][this.k][2]);
    }
  }
}